package co.fitstart.scheduler.job;

import java.util.Date;
import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import co.fitstart.scheduler.service.CourseRoundService;

@Component
public class CourseRoundJob {
    
    private static final Logger logger = LoggerFactory.getLogger(CourseRoundJob.class);
    
    @Autowired
    private CourseRoundService courseRoundService;
    
    /**
     * 
     * 
     * run at 2:10 am every day FIXME
     */
    @Scheduled(cron = "0 45 2 * * *")
    public void scanCourseRoundExpiry() {
        
        Date now = new Date();
        
        logger.info("start running CourseRoundJob#scanCourseRoundExpiry at {}", now);
        
        // scan order pay timeout
        int toExpireCourseRound = courseRoundService.toExpiryCourseRound(now);
        logger.info("scaned {} courseRound need to be expired before expire course round", toExpireCourseRound);
        
        if(toExpireCourseRound > 0) {
            courseRoundService.expriyCourseRound(now);
            
            toExpireCourseRound = courseRoundService.toExpiryCourseRound(now);
            logger.info("scaned {} courseRound need to be expired after expire courseRound", toExpireCourseRound);
        }
        
        logger.info("finish running CourseRoundJob#scanCourseRoundExpiry at {}", new Date());
        
    }
    
    /**
     * 更新为永久失效课程
     * 
     * run at 3:0 am every day FIXME
     */
    @Scheduled(cron = "0 0 3 * * *")
    public void scanCourseRoundForeverExpiry() {
        
        Date now = new Date();
        
        // 90天前
        Date nintyBefore = DateUtils.addDays(now, -90);
        
        logger.info("start running CourseRoundJob#scanCourseRoundExpiry at {}", now);
        
        // scan order pay timeout
        int toForverExpireCourseRound = courseRoundService.toForverExpiryCourseRound(nintyBefore);
        logger.info("scaned {} courseRound need to be forever expired before expire course round", toForverExpireCourseRound);
        
        if(toForverExpireCourseRound > 0) {
            courseRoundService.foreverExpriyCourseRound(nintyBefore);
            
            toForverExpireCourseRound = courseRoundService.toForverExpiryCourseRound(nintyBefore);
            logger.info("scaned {} courseRound need to be expired after expire courseRound", toForverExpireCourseRound);
        }
        
        logger.info("finish running CourseRoundJob#scanCourseRoundExpiry at {}", new Date());
        
    }
    
    /**
     * 
     * 
     * run at 0:30 am every day FIXME
     */
    @Scheduled(cron = "0 30 0 * * *")
    public void scanCourseRoundUnfreeze() {
        
        Date now = new Date();
        
        logger.info("start running CourseRoundJob#scanCourseRoundUnfreeze at {}", now);
        
        // scan order pay timeout
        int toUnfreezeCourseRound = courseRoundService.toUnfreezeCourseRound(now);
        logger.info("scaned {} courseRound need to be unfreezed before unfreeze course round", toUnfreezeCourseRound);
        if(toUnfreezeCourseRound > 0) {
            courseRoundService.unfreezeCourseRound(now);
            
            toUnfreezeCourseRound = courseRoundService.toUnfreezeCourseRound(now);
            logger.info("scaned {} courseRound need to be unfreezed after unfreeze courseRound", toUnfreezeCourseRound);
        }
        
        logger.info("finish running CourseRoundJob#scanCourseRoundUnfreeze at {}", new Date());
        
    }
    
    @Scheduled(cron = "0 0 2 * * *")
    public void scanCourseRoundStopCamp() {

        Date now = new Date();
        
        logger.info("start running CourseRoundJob#scanCourseRoundStopCamp at {}", now);
        
        // scan individual course round stop camp
        int toStopCampIndividualRound = courseRoundService.toStopCampIndividualRound();
        logger.info("scaned {} individualRound need to be stopped camp before stop camp individualCourseRound", toStopCampIndividualRound);
        
        if(toStopCampIndividualRound>0) {
            courseRoundService.stopCampIndividualRound();
            
            toStopCampIndividualRound = courseRoundService.toStopCampIndividualRound();
            logger.info("scaned {} individualRound need to be stopped camp after stop camp individualRound", toStopCampIndividualRound);
        }
        
        // scan year course round stop camp
        int toStopCampYearRound = courseRoundService.toStopCampYearRound(now);
        logger.info("scaned {} yearRound need to be stopped camp before stop camp yearRound", toStopCampYearRound);
        
        if(toStopCampYearRound>0) {
            courseRoundService.stopCampYearRound(now);
            
            toStopCampYearRound = courseRoundService.toStopCampYearRound(now);
            logger.info("scaned {} yearRound need to be stopped camp after stop camp yearRound", toStopCampYearRound);
        }
        
        logger.info("finish running CourseRoundJob#scanCourseRoundStopCamp at {}", new Date());
        
    }
    
    /**
     * 记录四周内未签到课程
     */
    @Scheduled(cron = "0 30 3 * * *")
    public void scanCourseRoundNosign() {
        Date now = new Date();
        
        logger.info("start running CourseRoundJob#scanCourseRoundNosign at {}", now);
        
        courseRoundService.recordNosigninCourseRound(now);
        
        logger.info("finish running CourseRoundJob#scanCourseRoundNosign at {}", new Date());
    }
}
